/*
 * Created on 01.02.2004
 *
 * To change the template for this generated file go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
package ro.qi.framework.sys.repository;

import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;

/**
 * @author Denverash
 *
 * To change the template for this generated type comment go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
public class EntityConfiguration {

	private String name;
	private String pkg;
	private String version;
	private String datasource;
	private Hashtable<String, ComponentConfiguration> components = new Hashtable<String, ComponentConfiguration>();
	private Hashtable<String, Vector<ActionConfiguration>> actions = new Hashtable<String, Vector<ActionConfiguration>>();

	public String toString() {
		StringBuffer tmp = new StringBuffer();
		tmp.append("\n\t\t" + pkg + "." + name + " v" + version + "\n\t\tdatasource = " + datasource);
		Enumeration enum1 = components.keys();
		while( enum1.hasMoreElements() ) {
			String name = (String)enum1.nextElement();
			ComponentConfiguration cc = (ComponentConfiguration)components.get(name);
			tmp.append("\n\t\t\tcomponent [name:" + cc.getName() + "][class:" + cc.getClassName() + "][field:" + cc.getFieldName() + "][default:" + cc.getDefaultValue() + "]");
		}
		Enumeration enum2 = actions.keys();
		while( enum2.hasMoreElements() ) {
			String name = (String)enum2.nextElement();
			Vector v = (Vector)actions.get(name);
			for( int idx = 0 ; idx < v.size() ; idx++ ) {
				ActionConfiguration ac = (ActionConfiguration)v.get(idx);
				tmp.append("\n\t\t\taction [name:" + ac.getName() + "][type:" + ac.getType() + "][content:" + ac.getContent() + "]");
			}
		}
		return tmp.toString();
	}

	public void addAction(ActionConfiguration actionConfiguration) {
		if( actions.containsKey(actionConfiguration.getName()) ) {
			Vector actionVector = (Vector)actions.get(actionConfiguration.getName());
			if( !actionVector.contains(actionConfiguration) )
				actionVector.add(actionConfiguration);
		} else {
			Vector actionVector = new Vector();
			actionVector.add(actionConfiguration);
			actions.put(actionConfiguration.getName(),actionVector);
		}
	}

	public Vector getActions(String actionName) {
		if( actions.containsKey(actionName) )
			return (Vector)actions.get(actionName);
		return new Vector();
	}

	public void addComponentConfiguration(ComponentConfiguration componentConfiguration) {
		components.put(componentConfiguration.getName(), componentConfiguration);
	}

	/**
	 * @return
	 */
	public String getDatasource() {
		return datasource;
	}

	/**
	 * @return
	 */
	public String getName() {
		return name;
	}

	/**
	 * @return
	 */
	public String getPackage() {
		return pkg;
	}

	/**
	 * @return
	 */
	public String getFullName() {
		if (pkg != null && pkg.length() > 0) {
			return pkg + "." + name;
		}
		return name;
	}

	/**
	 * @return
	 */
	public String getVersion() {
		return version;
	}

	/**
	 * @param string
	 */
	public void setDatasource(String string) {
		datasource = string;
	}

	/**
	 * @param string
	 */
	public void setName(String string) {
		int index = string.lastIndexOf('.');
		if (index >= 0) {
			if (index == (string.length() - 1)) {
				name = string.substring(0, index);
			} else {
				name = string.substring(index + 1);
				pkg = string.substring(0, string.length() - name.length() - 1);
			}
		} else {
			name = string;
		}
	}

	/**
	 * @param string
	 */
	public void setPackage(String string) {
		pkg = string;
	}

	/**
	 * @param string
	 */
	public void setVersion(String string) {
		version = string;
	}

	/**
	 * @return
	 */
	public Hashtable<String, ComponentConfiguration> getComponents() {
		return components;
	}

	/**
	 * @return
	 */
	public Hashtable<String, Vector<ActionConfiguration>> getActions() {
		return actions;
	}

	/**
	 * @param hashtable
	 */
	public void setActions(Hashtable<String, Vector<ActionConfiguration>> hashtable) {
		actions = hashtable;
	}

	/**
	 * @param hashtable
	 */
	public void setComponents(Hashtable<String, ComponentConfiguration> hashtable) {
		components = hashtable;
	}

}
